#include <stdio.h>
#include <math.h>

double myround(double x);
double myround2(double x);
int main(void)
{	
	double num,roundNum;
	printf("Please input the number you want to round:");
	scanf("%lf", &num);    // 这里竟然用%f，会出现严重的精度丢失问题。
	roundNum = myround2(num);
	printf("The num after round is %f.\n", roundNum);
	
	return 0;
}

double myround(double x)
{	
	printf("x = %f.\n",x);
	double y = (int)x + 0.0;
	printf("y = %f.\n",y);
	double z;
	if(x > 0.0)
		z = y+0.5;
	else 
		z = y - 0.5;
	if(x < z)
	{
		if(x > 0.0)
			return y;
		else 
			return y-1;
	} 
	else
	{
		if(x > 0.0)
			return y+1;
		else 
			return y;
	}
}

double myround2(double x)
{
	double x_ceil, x_floor;
	x_ceil = ceil(x);
	x_floor = floor(x);
	
	double x_mid = (x_ceil + x_floor)/2.0;
	if (x< x_mid)
	{
		return x_floor;
	}
	else
	{
		return x_ceil;
	}
}
